Send media events with InstantSearch
Collect user interactions as events on your media website.
Capturing real-time user interactions as events gives you actionable insights with click and conversion metrics, and they help you increase your customer engagement and conversions. Events are used to activate Algolia features and products like:
- NeuralSearch
- Algolia Recommend
- Personalization
- Dynamic Re-Ranking
- Query Categorization
- Search analytics
- Revenue analytics
- A/B testing.
Before you begin
To follow this guide, you need to build both search results and category pages with one of these UI libraries:
- InstantSearch.js version 4.59 or later
- React InstantSearch version 7.2.1 or later
- Vue InstantSearch version 4.12.0 or later
For other methods of collecting events, see Get started with events.
Manage the Insights library
InstantSearch loads the search-insights
library for you from jsDelivr.
You don’t need to install it or set it up yourself.
If you’re using a Content Security Policy to protect your site and you want to let InstantSearch load search-insights
for you, make sure to add https://cdn.jsdelivr.net
in your list of trusted sources for JavaScript.
If you prefer hosting your own version of search-insights
, you can add it to your project:
- Install the Insights client
- Initialize the Insights client (optional)
InstantSearch doesn’t load search-insights
when it detects it on the page.
Enable events collection
You can enable the automatic collection of events from your InstantSearch apps in the Algolia dashboard (without coding), or when setting up your InstantSearch app (with coding).
Enabling automatic events collection takes care of the following:
- Add the
search-insights
library to your project and make it globally available aswindow.aa
- Set an anonymous
userToken
for sending events to the Insights API and for search requests - Include the
queryID
parameter in the search response - Send default click and view events for your InstantSearch widgets
Go to the Events hub in the Algolia dashboard to check the default events arriving from your website or app. For more information, see Validate your events.
No code
- Go to the Algolia dashboard and select your Algolia application.
- On the left sidebar, select Data sources > Events > Settings.
- Click Enable automatic events collection.
Code
Enable the insights
option:
You may want to send events that aren’t automatically captured by InstantSearch widgets.
Understand which events to track
To understand which events you should send, think about the different steps a user takes through your website to reach the final goal: the product purchase.
Starting with a search on any of your pages, a user might take the following actions:
- Select a product from the search results to open the content details page.
- Consume content, such as watching a video, reading an article, or listening to an audio track.
- Interact with content, such as liking, subscribing, or following.
Starting with a search on any of your pages, a user might take the following actions:
- Select a product from the search results to open the content details page.
- Consume content, such as watching a video, reading an article, or listening to an audio track.
- Interact with content, such as liking, subscribing, or following.
Starting with a visit to your homepage or to a category page, a user might take the following actions:
- Select a content category to open a category page.
- Select content displayed on the page to open the content details page.
- Consume content, such as watching a video, reading an article, or listening to an audio track.
- Interact with content, such as liking, subscribing, or following.
Starting with a search on an internet search engine, with a click on a recommendation, or any other external referral, a user might take the following actions:
- Click on an (internet) search result, recommendation, or other link to your website.
- Depending on where the user lands on your website, they continue the same path as if they started with your homepage or with a category page.
Search and search results pages
User action | Method name | Automatically collected? |
---|---|---|
User clicks search result | clickedObjectIDsAfterSearch | Yes |
User clicks categories/filters | clickedFilters | Yes |
User views search results | viewedObjectIDs | Yes |
Browse/category page
User action | Method name | Automatically collected? |
---|---|---|
User clicks content | clickedObjectIDsAfterSearch | Yes |
User views content | viewedObjectIDs | Yes |
Content details page
User action | Method name | Automatically collected? |
---|---|---|
User views content | viewedObjectIDs | No |
Keep track of query IDs
Conversion events are often triggered on pages that aren’t covered by InstantSearch, such as your content details pages.
To relate the conversion events back to the search request made on your search results or category pages, you need to keep track of the query IDs across your pages.
Track conversion events
Using InstantSearch widgets
You can use the sendEvent
function to send conversion events from your InstantSearch app.
click
events, setting custom conversion
events doesn’t prevent the custom click
event from being sent.On pages without InstantSearch widgets
An example of a conversion event that may be relevant to your media site:
The window.aa
object is the API client for the Insights API and is globally available
if you enabled automatic events collection.
In convertedObjectIDsAfterSearch
:
queryID
is used by Algolia to relate the event to a prior search.objectIDs
indicates which items were part of the conversion.TheobjectID
parameter is included in the search response for each hit.
If the event isn’t directly related to a search, for example, it’s triggered on the homepage,
use the convertedObjectIDs
method instead.
Some other examples of conversion events are:
Article Bookmarked
: a user bookmarked an article.Article Liked
: a user liked an article.Article Recommended
: a user recommended an article.Playlist Followed
: a user followed a playlist.Author Followed
: a user followed an author.Brand Followed
: a user followed a brand.
Track click events
Override default click events
The hits
and infiniteHits
widgets expose a sendEvent
function.
Use it to send click
events when users interact with your search results.
You can set more events on specific parts of your template. In the following example, when clicking on the View more info button, two events are sent to the Insights API:
- A
click
event with theeventName
“Article More Info” - A
click
event with theeventName
“Article Clicked” (through event propagation)
To only send the most specific event per clicked element, you can use Event.stopPropagation
in your event handler.
When InstantSearch captures a custom click
event that you defined, it doesn’t send the default click
event.
In the following example, when clicking the View mores button, only the “Article More Info Clicked” event is sent.
Track additional click events
For Algolia Recommend and Personalization, capture additional click events when users select individual products:
- On your home page
- From product recommendations.
To send click events with the Insights client, add the following code whenever a user clicks on an article---for example, on your home page.
(Optional) Set up Personalization
For effective personalization, you need to identify users across sessions. It’s best to use an identifier from your authentication system after users signed in. For more information, see User token.
After getting the identifier from your system,
set it as the authenticatedUserToken
parameter.
If you can’t get persistent user identifiers from your system, you can store the anonymous user token in a cookie after obtaining user consent.
If you don’t use persistent user identifiers, a new anonymous user token is generated on every page refresh.
Personalization benefits from the same click and conversion events, plus it can use view events to enrich user profiles.
Your InstantSearch components for the search results and category pages automatically collect view events.
To capture additional view events, such as on your homepage, add this code: